package com.itheima.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import redis.clients.jedis.JedisPool;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  权限控制过滤器，不登录不给访问
 */

public class LoginFilter implements Filter {

    private JedisPool jedisPool;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext servletContext = filterConfig.getServletContext();
        WebApplicationContext con = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        jedisPool = (JedisPool) con.getBean("jedisPool");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        // 定义HTTPServlet请求和响应对象
        HttpServletRequest request = null;
        HttpServletResponse response = null;
        try {
            // 类型强转
            request = (HttpServletRequest) req;
            response =(HttpServletResponse) resp;

            // 获取uri
            String uri = request.getRequestURI();
            System.out.println(uri);
            // 放行白名单
            // 1. 放行后缀：js,png,css
            // 2. 放行登录页
            if (uri.endsWith("login.html")
                    || uri.endsWith(".css")
                    || uri.endsWith("js")
                    || uri.endsWith(".png")
                    || uri.endsWith(".jpg")
                    || uri.endsWith(".ico")
                    || uri.endsWith(".ttf")
                    || uri.endsWith(".woff")
                    || uri.endsWith("send4Login.do")
                    || uri.endsWith("login.do")
            ){
                // 放行
                chain.doFilter(request,response);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 获取Cookie
        Cookie[] cookies = request.getCookies();
        boolean flag = false;  // 标记，默认login_member_telephone为空
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie.getName().equals("login_member_telephone")){
                String value = cookie.getValue();
                // 从redis根据cookie里login_member_telephone值取redis的值
                String login_member_telephone = jedisPool.getResource().get(value);
                if (login_member_telephone != null && login_member_telephone.length()>0){
                    // 取到放行
                    chain.doFilter(request,response);
                    flag = true;
                    return;
                }
            }
        }

        if (!flag) {
            // login_member_telephone值是否为空，为空跳转到登录页
            request.getRequestDispatcher(request.getContextPath() + "/pages/login.html").forward(request, response);
        }
    }

    @Override
    public void destroy() {

    }
}
